﻿using System;
using System.Data;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.ClientTasks.Attributes;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using CdcSoftware.Pivotal.Engine.UI.DataTypes;
using CdcSoftware.Pivotal.Engine.Types.Database;
using CdcSoftware.Pivotal.Engine.Types.DataTemplates;
using CdcSoftware.Pivotal.Engine.Types.Localization;
using CdcSoftware.Pivotal.Applications.Core.Common;
using System.Windows.Forms;

namespace Pcm.Retail.Client.NotificationHandlers
{
    public partial class ContactNotificationHandler
    {
        /// <summary>
        /// Handles OnFormIdRequested notification on Contact Table to set the form name for a given record id.
        /// </summary>
        /// <history>
        /// Revision#      Date            Author          Description
        /// 6.0.8.1        2011-05-18      DS              Initial version for Retail.
        /// </history>
        [NotificationEvent("OnFormIdRequested", Table = "Contact")]
        public FormIdRequestReturnValue OnContactFormIdRequested(Id recordId, Table table)
        {
            try
            {
                FormIdRequestReturnValue retVal = new FormIdRequestReturnValue();
                IDataAccessService dataAcess = ClientContext.ClientAPIService.GetService<IDataAccessService>();
                //ILanguageDictionaryService ldService = ClientContext.ClientAPIService.GetService<ILanguageDictionaryService>();

                string formName = string.Empty;
                if (recordId == null)
                {
                    //DS:29102012 - Added Check for the Mobile system. Do not allow the user to create the Contact when System is Offline.
                    if (Pcm.Retail.Client.Globals.SystemClient.SystemInformation.IsMobile)
                    {
                        PivotalMessageBox.Show("This feature is available only in Online mode.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        retVal.CancelFormIdRequest = true;
                        return retVal;
                    }
                    if (Globals.CurrentUserInSecurityGroup("Retail POS"))
                    {
                        //formName = "RE_Member_Create";
                        //DS:10102012 - Depending upon the Create Member Form name dedfined in the LOB, show the client form.
                        Id currEmpLOB = Id.Create(Globals.SqlIndex("Employee", "RE_Line_Of_Business_Id", Globals.SystemClient.UserProfile.EmployeeId));
                        String createMemberFormName = TypeConvert.ToString(Globals.SqlIndex("RE_Line_Of_Business", "Create_Member_Form", currEmpLOB));

                        if (!String.IsNullOrEmpty(createMemberFormName))
                            formName = createMemberFormName;
                        else
                            PivotalMessageBox.Show("Create Member form is not defined for the LOB." + Environment.NewLine +
                                "Please contact System Administrator.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    //DS: 11072013 - For Admin, open up the RE_Member form.
                    else if (Globals.CurrentUserInSecurityGroup("Retail Administrator"))
                        formName = "RE_Member";
                    else
                        formName = "RE_Contact";
                }
                else
                {
                    //Get Contact type to check which form to open.
                    string type = TypeConvert.ToString(Globals.ServerSqlIndex("Contact", "RE_Type", recordId));

                    //Default to the RE_Contact form if it is an unknown type. 
                    switch (type)
                    {
                        case "Member":
                            if (Globals.CurrentUserInSecurityGroup("Retail POS"))
                                formName = "RE_Member-ReadOnly";
                            else
                                formName = "RE_Member";
                            break;
                        default:
                            formName = "RE_Contact";
                            break;
                    }
                }                

                if (!String.Equals(formName, string.Empty))
                    retVal.FormId = dataAcess.SystemClient.UserProfile.GetMetaItem<CdcSoftware.Pivotal.Engine.Client.Types.Form>(formName).Id;
                else
                    retVal.FormId = null;

                return retVal;
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
                return null;
            }
        }
    }
}